import Vue from 'vue'

/**
 * 增加千分位分隔符
 */
Vue.filter('formatNumber', function (value: any) {
  let num = '0'
  let start = 0
  if (value) {
    if (value.toString().indexOf('%') > -1) {
      return value
    } else {
      const valueArr = value.toString().split('')
      num = value.toString()
      if (num.length > 3) {
        const len = num.length
        let splitNum = 0
        if (len % 3 === 0) {
          splitNum = len / 3 - 1
        } else {
          splitNum = Math.floor(len / 3)
        }
        if (len % 3 === 0) {
          start = 2
        } else if ((len + 1) % 3 === 0) {
          start = 1
        } else if ((len - 1) % 3 === 0) {
          start = 0
        }
        for (let i = splitNum; i > 0; i--) {
          valueArr.splice(start + (3 * (i - 1)) + 1, 0, ',')
        }
        return valueArr.join('')
      } else {
        return value
      }
    }
  } else {
    return value
  }
})
